package com.jh.web.radar;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.jh.pojo.Entity;
import com.jh.pojo.cmd.ResideDetectCmdBean;
import com.jh.utils.BlockingQueueUtils;

/**
 * Servlet implementation class ResideDetectServlet
 */
@WebServlet(description = "Reside Detect", urlPatterns = { "/ResideDetectServlet" })
public class ResideDetectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static final Logger logger = LoggerFactory.getLogger(ResideDetectServlet.class);

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResideDetectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset:utf-8");
        String ipAddr = request.getParameter("ipaddr");
        String posAngle = request.getParameter("posAngle");

        logger.debug("--->" + "ipAddr: " + ipAddr + " posAngle: " + posAngle);
        BlockingQueueUtils queueUtils = BlockingQueueUtils.getInstance();

        ArrayBlockingQueue<String> txBq = queueUtils.gettxBq();
        Gson gson = new Gson();
        ResideDetectCmdBean resideDetectCmdBean = new ResideDetectCmdBean();
        Entity entity = new Entity();
        resideDetectCmdBean.setIp(ipAddr);
        resideDetectCmdBean.setPosAngle(Integer.parseInt(posAngle));

        entity.setType(22);
        entity.setData(resideDetectCmdBean);
        String json = gson.toJson(entity);
        logger.debug("--->" + " reside Detect json: " + json);
        try {
            txBq.put(json);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);

        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
